﻿using Newtonsoft.Json;
using System.IO;
using VM.Common.Enums;
using VM.Dialogs.Views;
using VM.Start.Common.Provide;

namespace VM.Start.Common.Helper
{
    public class SerializeHelp
    {
        public static T Deserialize<T>(string fileName, bool isLoadCopyFile = false)
        {
            T t = default(T);
            try
            {
                if (!File.Exists(fileName))
                {
                    File.Create(fileName).Close();
                }
                FileInfo fileInfo = new FileInfo(fileName);
                if (fileInfo.Length == 0 && isLoadCopyFile)//文件内容为空
                {
                    int startIndex = fileName.LastIndexOf(".");
                    string fileCopyName = fileName.Insert(startIndex, "_Copy");
                    if (File.Exists(fileCopyName))
                    {
                        File.Copy(fileCopyName, fileName, true);
                        return (T)JsonConvert.DeserializeObject<T>(File.ReadAllText(fileCopyName));
                    }
                    else
                    {
                        return t;
                    }
                }
                else
                {
                    return (T)JsonConvert.DeserializeObject<T>(File.ReadAllText(fileName));
                }
            }
            catch (Exception e)
            {
                return t;
            }
        }
        public static void SerializeAndSaveFile<T>(T obj, string fileName, bool isCreatCopyFile = false)
        {
            try
            {
                //当项目比较大的时候保存耗时较长，这个时候如果异常断电，那么项目文件会全部丢失，为解决此问题：先序列化一个临时项目文件，序列化成功后再移动替换原文件
                if (isCreatCopyFile)
                {
                    int startIndex = fileName.LastIndexOf(".");
                    string fileCopyName = fileName.Insert(startIndex, "_Copy");
                    File.WriteAllText(fileCopyName, JsonConvert.SerializeObject(obj));
                    File.Copy(fileCopyName, fileName, true);
                }
                else
                {
                    File.WriteAllText(fileName, JsonConvert.SerializeObject(obj));
                }
            }
            catch (Exception e)
            {
                Logger.GetExceptionMsg(e);
            }
        }
        public static T Clone<T>(T obj)
        {
            return (T)JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(obj));
        }
        public static void BinSerializeAndSaveFile<T>(T obj, string fileName)
        {
            FileStream stream = null;
            try
            {
                var settings = new JsonSerializerSettings
                {
                    ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                    TypeNameHandling = TypeNameHandling.Auto,
                    NullValueHandling = NullValueHandling.Ignore
                };
                // 保存为JSON格式
                File.WriteAllText(fileName, JsonConvert.SerializeObject(obj, settings));
                MessageView.Ins.MessageBoxShow("解决方案保存成功！", eMsgType.Success);
            }
            catch (Exception e)
            {
                Logger.GetExceptionMsg(e);
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }
        }
        public static T BinDeserialize<T>(string fileName)
        {
            T t = default(T);
            try
            {
                if (!File.Exists(fileName))
                {
                    return t;
                }
                var settings = new JsonSerializerSettings
                {
                    ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                    TypeNameHandling = TypeNameHandling.Auto,
                    NullValueHandling = NullValueHandling.Ignore
                };

                // 读取并反序列化JSON文件
                string jsonContent = File.ReadAllText(fileName);
                return JsonConvert.DeserializeObject<T>(jsonContent, settings);
            }
            catch (Exception e)
            {
                Logger.GetExceptionMsg(e);
                return t;
            }
        }

    }
}
